package aleksic.videogrid
{
	import com.greensock.events.LoaderEvent;
	import com.greensock.loading.VideoLoader;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.ColorTransform;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import net.stevensacks.preloaders.CircleSlicePreloader;
	
	public class Player extends Sprite
	{
	
		public var loader:VideoLoader;
		
		public var vars:VideoGridFlashVars;
		
		private var spinner:CircleSlicePreloader;
		
		private var statusTF:TextField;
		
		private var videoMask:Sprite;
		
		public function Player(vars:VideoGridFlashVars)
		{
			super();
			this.vars = vars;
			
			if (vars.debug) {
				var fmt:TextFormat = new TextFormat();
				fmt.font = 'Arial';
				fmt.size = 8;
				fmt.color = 0x000000;
				
				statusTF = new TextField();
				statusTF.background = true;
				statusTF.autoSize = TextFieldAutoSize.LEFT;
				statusTF.defaultTextFormat = fmt;
				addChild(statusTF);
			}
			addEventListener(Event.ADDED_TO_STAGE, handleAdded);
		}
		
		public function get videoUrl():String
		{
			return vars.files[parent.getChildIndex(this)];
		}
		protected function handleAdded(event:Event):void
		{
			load( videoUrl );
		}		
		
		public function load(url:String):void
		{

			spinner = new CircleSlicePreloader();
			spinner.x = 0.5 * vars.itemWidth;
			spinner.y = 0.5 * vars.itemHeight;
			addChild(spinner);
			
			if (loader) {
				loader.content.mask = null;
				loader.cancel();
				loader.dispose(true);
				removeChild(videoMask);
			}
			 
			loader = new VideoLoader(url, {
				width: vars.itemWidth,
				height: vars.itemHeight,
				bgColor: vars.bgColor,
				bgAlpha : vars.bgAlpha,
				autoPlay: vars.videoAutoPlay && !vars.syncStart,
				scaleMode: vars.videoScaleMode,
				bufferTime: vars.videoBufferTime,
				container: this,
				requireWithRoot: this.root,
				onProgress: handleLoadProgress,
				onComplete: handleLoadDone,
				onError: handleLoadFail,
				repeat: -1
			});
			loader.addEventListener(VideoLoader.VIDEO_BUFFER_FULL, handleBufferFull);
			loader.addEventListener(VideoLoader.VIDEO_PLAY, handlePlay);
			loader.load();
			
			videoMask = new Sprite();
			videoMask.graphics.beginFill(0,1);
			videoMask.graphics.drawRect(0, 0, vars.itemWidth, vars.itemHeight);
			addChild(videoMask);
			
			loader.content.scaleX = loader.content.scaleY = vars.itemScale;
			loader.content.x = vars.itemOffsetX;
			loader.content.y = vars.itemOffsetY;
			loader.content.mask = videoMask;
			if (statusTF) {
				addChild(statusTF);
			}
		}
		
		
		public function handleLoadProgress(e:LoaderEvent):void
		{
			if (vars.debug) {
				statusTF.text = videoUrl+': '+Math.round(100 * e.target.progress) + '%';
				if (e.target.progress === 1) {
					removeChild(statusTF);
				}
			}
		}
		
		protected function handleBufferFull(event:Event):void
		{
			dispatchEvent(event);
		}
		
		protected function handlePlay(event:Event):void
		{
			if (contains(spinner)) {
				removeChild(spinner);
			}
			if (vars.itemToggleOnClick) {
				addEventListener(MouseEvent.CLICK, handleClick);
			}
		}
		
		public function handleLoadDone(e:LoaderEvent):void
		{
			if (contains(spinner)) {
				removeChild(spinner);
			}
			trace(videoUrl,'loaded');
		}
		
		public function handleLoadFail(e:LoaderEvent):void
		{
			var clr:ColorTransform = new ColorTransform();
			clr.color = 0xFF0000;
			spinner.transform.colorTransform = clr;
			trace(videoUrl,'failed');
		}
		
		protected function handleClick(event:MouseEvent):void
		{
			toggle();
		}
		
		public function play():void
		{
			loader.playVideo();
		}
		
		public function pause():void
		{
			loader.pauseVideo();	
		}
		
		public function toggle():void {
			loader.videoPaused = !loader.videoPaused;
		}
	}
}